// 
// J2CsMapping : runtime library for J2CsTranslator
// 
// Copyright (c) 2008-2010 Alexandre FAU.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
// Contributors:
//   Alexandre FAU (IBM)
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ILOG.J2CsMapping.Reflect
{
    /// <summary>
    /// Utility method for java.lang.relfect API
    /// </summary>
    public class Helper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="paramsType"></param>
        /// <returns></returns>
        public static ConstructorInfo GetConstructor(Type type, params Type[] paramsType)
        {
            if (paramsType.Length == 0)
            {
                return type.GetConstructor(new Type[0]);
            }
            else
            {
                return type.GetConstructor(paramsType);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <param name="paramsType"></param>
        /// <returns></returns>
        public static MethodInfo GetMethod(Type type, String name, params Type[] paramsType)
        {
            char upper = name.Substring(0, 1).ToUpper()[0];
            String upperMethodName = upper + name.Remove(0, 1);
            if (paramsType.Length == 0)
            {
                return type.GetMethod(upperMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            }
            else
            {
                return type.GetMethod(upperMethodName, paramsType);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cInfo"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T Invoke<T>(ConstructorInfo cInfo, params Object[] parameters)
        {
            return (T)cInfo.Invoke(parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cInfo"></param>
        /// <param name="obj"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T Invoke<T>(MethodInfo cInfo, Object obj, params Object[] parameters)
        {
            return (T)cInfo.Invoke(obj, parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cInfo"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static Object Invoke(ConstructorInfo cInfo, params Object[] parameters)
        {
            return cInfo.Invoke(parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cInfo"></param>
        /// <param name="obj"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static Object Invoke(MethodInfo cInfo, Object obj, params Object[] parameters)
        {
            return cInfo.Invoke(obj, parameters);
        }

        
    }
}
